//
//  YDAlertViewController.m
//  YunDongGym
//
//  Created by lqq on 2018/6/4.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDAlertViewController.h"
@interface YDAlertViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, copy) void(^cancleBlock)(void);
@property (nonatomic, copy) void(^confimBlock)(void);
@property (nonatomic, strong) NSString *message;
@property (nonatomic, copy) NSString *cancleTitle;//
@property (nonatomic, copy) NSString *confimTitle;//
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, assign) NSTextAlignment textAligment;
@end
@implementation YDAlertViewController

+ (instancetype)alertViewControllerWithMessage:(NSString *)message
                             cancleTitle:(NSString *)cancleTitle
                             confimTitle:(NSString *)confimTitle
                                   cancleBlock:(void(^)(void))cancleBlock
                                   confimBlock:(void(^)(void))confimBlock
{
    return [YDAlertViewController alertViewControllerWithMessage:message cancleTitle:cancleTitle confimTitle:confimTitle textAligment:NSTextAlignmentCenter cancleBlock:cancleBlock confimBlock:confimBlock];
}

+ (instancetype)alertViewControllerWithMessage:(NSString *)message
                                   cancleTitle:(NSString *)cancleTitle
                                   confimTitle:(NSString *)confimTitle
                                  textAligment:(NSTextAlignment)textAligment
                                   cancleBlock:(void(^)(void))cancleBlock
                                   confimBlock:(void(^)(void))confimBlock
{
    YDAlertViewController *alertView = [[YDAlertViewController alloc] init];
    alertView.message = message;
    alertView.cancleTitle = cancleTitle;
    alertView.confimTitle = confimTitle;
    alertView.cancleBlock = cancleBlock;
    alertView.confimBlock = confimBlock;
    alertView.textAligment = textAligment;
    [alertView show];
    return alertView;
}

- (void)show
{
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 2;
    contentView.layer.masksToBounds = YES;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(25);
        make.right.equalTo(self).offset(-25);
        make.centerY.equalTo(self);
    }];
    self.contentView = contentView;
    
    UILabel *messageLabel = [[UILabel alloc] init];
    messageLabel.textColor = HEXColor(@"#595959");
    messageLabel.font = RegularFont(16);
    messageLabel.numberOfLines = 0;
    messageLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(33);
        make.right.equalTo(contentView).offset(-33);
        make.top.equalTo(contentView).offset(35);
        make.bottom.equalTo(contentView).offset(-92);
    }];
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_message];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    // 设置文字居中
    paragraphStyle.alignment = _textAligment;
    [paragraphStyle setLineSpacing:8];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_message length])];
    messageLabel.attributedText = attributedString;
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancleBtn setTitle:_cancleTitle forState:UIControlStateNormal];
    [cancleBtn setTintColor:HEXColor(@"#4A4A4A")];
    cancleBtn.layer.cornerRadius = 2;
    [cancleBtn addBorderWithBorderWidth:kOnePX borderColor:HEXColor(@"#DFDFDF")];
    [contentView addSubview:cancleBtn];
    [cancleBtn addTarget:self action:@selector(cancleBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(contentView).offset(-15);
        make.left.equalTo(contentView).offset(24);
        make.height.equalTo(@(44));
        if (self.cancleTitle.length == 0) {
            make.width.equalTo(@(0));
        }
    }];
    
    UIButton *confimBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [confimBtn setTitle:_confimTitle forState:UIControlStateNormal];
    [confimBtn setTintColor:[UIColor whiteColor]];
    confimBtn.backgroundColor = HEXColor(@"#3AA7FF");
    confimBtn.layer.cornerRadius = 2;
    confimBtn.layer.masksToBounds = YES;
    [contentView addSubview:confimBtn];
    [confimBtn addTarget:self action:@selector(confimBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [confimBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.height.equalTo(cancleBtn);
        make.right.equalTo(contentView).offset(-24);
        
        if (self.cancleTitle.length == 0) {
            make.left.equalTo(cancleBtn.mas_left);
            cancleBtn.hidden = YES;
        } else {
            make.width.equalTo(cancleBtn);
            make.left.equalTo(cancleBtn.mas_right).offset(6);
        }

    }];
    
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
    
    
    if (_cancleTitle.length == 0) {
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] init];
        [tapGes addTarget:self action:@selector(handleGesture:)];
        tapGes.delegate = self;
        [self addGestureRecognizer:tapGes];
    }
}

- (void)cancleBtnClicked
{
    if (_cancleBlock) {
        _cancleBlock();
    }
    [self removeFromSuperview];
}

- (void)confimBtnClicked
{
    if (_confimBlock) {
        _confimBlock();
    }
    [self removeFromSuperview];
}

- (void)handleGesture:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, point)) {
        [self removeFromSuperview];
    }
}


@end
